package com.briup.jz.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.jz.bean.AccountApply;
import com.briup.jz.bean.AccountApplyExample;
import com.briup.jz.bean.AccountEmployee;
import com.briup.jz.bean.AccountEmployeeExample;
import com.briup.jz.bean.extend.AccountCustomerExtend;
import com.briup.jz.bean.extend.AccountEmployeeExtend;
import com.briup.jz.dao.AccountEmployeeMapper;
import com.briup.jz.dao.extend.AccountEmployeeExtendMapper;
import com.briup.jz.service.AccountEmployeeService;
import com.briup.jz.utils.CustomerException;
@Service
public class AccountEmployeeServiceImpl implements AccountEmployeeService{

	@Autowired
	private AccountEmployeeMapper accountemployeeMapper;

	@Autowired
	private AccountEmployeeExtendMapper accountemployeeExtendMapper;

	@Override
	public void saveOrUpdate(AccountEmployee accountemployee) throws CustomerException {
		// TODO Auto-generated method stub
		if(accountemployee.getId()!=null) {
			accountemployeeMapper.updateByPrimaryKey(accountemployee);
		}else {
			// 判断是否有同名，如果有抛出异常
//			AccountEmployeeExample example = new AccountEmployeeExample();
//			example.createCriteria().andIdEqualTo(AccountEmployee.getId());
//			List<AccountEmployee> list = accountemployeeMapper.selectByExample(example);
//			if(list.size()>0) {
//				throw new CustomerException("同名已存在");
			accountemployee.setStatus("正常");
			accountemployee.setTransferTime((new Date().getTime()));
			accountemployeeMapper.insert(accountemployee);
			}
	}
	
	@Override
	public List<AccountEmployee> query(Long id,String status, String type, Long userId) {
		/*模板*/
		AccountEmployeeExample example=new AccountEmployeeExample();
		/*条件*/
		AccountEmployeeExample.Criteria criteria = example.createCriteria();
		if(id!=null) {
			criteria.andIdEqualTo((long)id);
		}
		if(status!=null) {
			criteria.andStatusEqualTo(status);
		}
		if(type!=null) {
			criteria.andTypeEqualTo(type);
		}
		if (userId!=null) {
			criteria.andUserIdEqualTo(userId);
		}
		List<AccountEmployee> list = accountemployeeMapper.selectByExample(example);
		/*for(AccountEmployee a:list) {
			System.out.println(a);
		}*/
//		System.out.println("zzzzzz");
		return list;
	}

	@Override
	public void deleteById(long id) throws CustomerException {
		// TODO Auto-generated method stub
		AccountEmployee address=accountemployeeMapper.selectByPrimaryKey(id);
		if(address==null) {
			throw new CustomerException("要删除不存在");
		}
		accountemployeeMapper.deleteByPrimaryKey(id);
	}
	
//  级联多符合查询
	@Override
	public List<AccountEmployeeExtend> queryCascade(String status, String type, Long userId) {
		return accountemployeeExtendMapper.select(status, type, userId);

	}
}
